<!-- TMS订单管理 - 综合表单页面 -->
<template>
  <div class="page-container">
    <page_breadcrumb />
    <el-radio-group v-if="isArtery" v-model="tmsWaybillCategory" @change="reload">
          <el-radio-button label="DOMESTIC_WAYBILL">{{ $t('label_waybill_domestic') }}</el-radio-button>
          <el-radio-button label="INTERNATIONAL_WAYBILL">{{ $t('label_waybill_international') }}</el-radio-button>
    </el-radio-group>
    <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e"
      :cell-class-name="cellClassName">
      <template v-slot:table-top>

      </template>
    </ex_super_table_page>

    <waybill_break_qty ref="waybill_break_qty" @submitSuccess="reload()" />
    <waybill_break_line ref="waybill_break_line" @submitSuccess="reload()" />
    <waybill_transfer_peer ref="waybill_transfer_peer" @submitSuccess="reload()" />
    <waybill_merge_scheduling ref="waybill_merge_scheduling" @submitSuccess="reload()" />
    <waybill_file_upload ref="waybill_file_upload" @submitSuccess="reload()" ></waybill_file_upload>
  </div>
</template>
<script>

let moduleName = "label_tms_waybill_tms_waybill_manager"
import waybill_break_qty from '@/component/tms/waybill_break_qty'
import waybill_break_line from '@/component/tms/waybill_break_line'
import waybill_transfer_peer from '@/component/tms/waybill_transfer_peer'
import waybill_merge_scheduling from '@/component/tms/waybill_merge_scheduling'
import waybill_file_upload from '@/component/tms/waybill_file_upload'

import superTablePageFunction from '@/mixin/super-table-page-function';
import router_params from '@/mixin/router_params';

const filterUploadName = {
        AIR_WAYBILL:'上传预配舱回执',
        SEA_WAYBILL:'上传订舱单',
        RAILWAY_WAYBILL:'上传铁路提单'
      }
export default {
  title: moduleName,
  components: { waybill_break_qty, waybill_break_line, waybill_transfer_peer, waybill_merge_scheduling,waybill_file_upload },
  mixins: [superTablePageFunction, router_params,],
  props:{
    waybillCategory:{
      type:String,
      default:''
    },
    isArtery:{
      type:Boolean,
      default:false
    },
  },
  watch:{
    waybillCategory:{
      handler(val){
        this.tmsWaybillCategory = val
      },
      immediate:true
    }
  },
  data() {
    return {
      tmsWaybillCategory: null,
      selection: [],
      option: {
        //搜索框内容
        search: {
          //搜索框内容，key为搜索字段，label为标签提示
          model: [
            { key: 'waybillCode', label: 'label_tms_waybill_waybill_number' },
            { key: 'refOrderCode', label: 'label_tms_receipt_order_platform_number' },
            { key: 'trackNumber', label: 'label_tms_receipt_shipping_order_number' },
            { key: 'receiptUserName', label: 'label_tms_receipt_consignee_name' },
            { key: 'customerName', label: 'label_tms_track_customer_name' },
            //干线,空运,海运,铁运
            {
              key: 'wmsOrderStatusEnum', label: 'label_tms_waybill_wms_order_status', type: 'select', enum: 'WmsOrderStatusEnum',
              if: () => this.tmsWaybillCategory != "DELIVERY_WAYBILL",
              listHandler: list => {
                if(this.tmsWaybillCategory == "DISTRIBUTION_WAYBILL"){
                  return list.filter(it => it == 'OUT_WAIT_OUTBOUND' || it == 'OUT_WAIT_CHECKOUT' || it == 'OUT_COMPLETE_OUTBOUND')
                }else if(this.tmsWaybillCategory == "DOMESTIC_WAYBILL" || this.tmsWaybillCategory == "INTERNATIONAL_WAYBILL"){
                  return list.filter(it => it == 'IN_TRANSIT_WAIT_INBOUND' || it == 'IN_TRANSIT_WAIT_OUTBOUND')
                }
              }
            },
            {
              key: 'status', label: 'label_order_status', type: 'select', enum: 'TmsWaybillStatusEnum',
            },
            { key: 'originAddress', label: 'label_tms_start_address', fold: true },
            { key: 'destinationAddress', label: 'label_tms_waybill_end_addr', fold: true },
            { key: 'isReceipt', label: 'label_tms_waybill_waybill_upload', type: 'select', enum: "YesOrNoEnum", fold: true },
            { key: 'esCarrierName', label: 'label_tms_assigned_carrier', fold: true },
          ],
        },
        actionExtra: [
          { label: 'label_tms_waybill_trade_transfer', attrs: { type: "primary" }, click: this.clickTransferToPeer },
          { label: 'label_tms_waybill_merge_scheduling', attrs: { type: "primary" }, click: ()=>this.clickMergeScheduling() },
          { label: 'label_tms_waybill_batch_scheduling', attrs: { type: "primary" }, click: this.clickBatchScheduling },
          { label: 'label_tms_waybill_cancel_scheduling', attrs: { type: "danger" }, click: this.clickRecallScheduling },
          { label: 'label_export', attrs: { type: "primary" }, click: this.exportExcel },

        ],
        table: {
          url: "/tms-api/waybill/TmsWaybillInfoQueryRpc/selectByPage.json",
          urlParams: (params) => {
            return Object.assign({
              tmsWaybillCategory: this.tmsWaybillCategory
            }, params)
          },
          model: [
            { type: "selection", clickToggle: true, selectable: this.selectable },
            { key: 'waybillCode', label: 'label_tms_receipt_transport_plan_number', 'min-width': 180, 'show-overflow-tooltip': true, },
            { key: 'trackNumber', label: 'label_order_track_code', 'min-width': 150, 'show-overflow-tooltip': true, },
            {
              key: 'transportSection', label: 'label_tms_waybill_waybill_type', 'min-width': 100, 'show-overflow-tooltip': true,
              formatter: (r, c, v, i) => this.$enumShow(v, "TransportSectionEnum")
            },
            {
              key: 'transportType', label: 'label_tms_waybill_transport_mode', 'min-width': 80, 'show-overflow-tooltip': true,
              formatter: (r, c, v, i) => this.$enumShow(v, "LogisticsProductsTypeEnum")
            },
            {
              key: 'status', label: 'label_status', 'width': 80, 'show-overflow-tooltip': true,
              formatter: (r, c, v, i) => this.$enumShow(v, "TmsWaybillStatusEnum")
            },

            {
              key: 'wmsOrderStatusEnum', label: 'label_tms_waybill_wms_order_status', 'width': 100, 'show-overflow-tooltip': true,
              if: () => this.tmsWaybillCategory != "DELIVERY_WAYBILL",
              formatter: (r, c, v, i) => this.$enumShow(v, "WmsOrderStatusEnum")
            },

            { key: 'esCarrierName', label: 'label_tms_waybill_carrier_name', 'width': 120, 'show-overflow-tooltip': true, },
            { key: 'customerName', label: 'label_customer_name', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'receiptUserName', label: 'label_tms_receipt_consignee_name', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'receiptContacts', label: 'label_tms_track_consignee_phone', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'startAddress', label: 'label_tms_waybill_original_place', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'endAddress', label: 'label_tms_receipt_destination', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'totalBox', label: 'label_tms_waybill_total_cartons', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'totalQuantity', label: 'label_tms_waybill_total_unit_quantity', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'totalWeight', label: 'label_total_weight', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'totalVolume', label: 'label_tms_waybill_total_volume', 'width': 100, 'show-overflow-tooltip': true, },
            { key: 'wmsPackagingQty', label: 'label_tms_waybill_total_wmsPackagingQty', 'width': 100, 'show-overflow-tooltip': true, if: () => this.tmsWaybillCategory != "DELIVERY_WAYBILL",},
            { key: 'wmsBoxQty', label: 'label_tms_waybill_total_wmsBoxQty', 'width': 100, 'show-overflow-tooltip': true, if: () => this.tmsWaybillCategory != "DELIVERY_WAYBILL",},
            { key: 'wmsWeight', label: 'label_tms_waybill_total_wmsWeight', 'width': 100, 'show-overflow-tooltip': true, if: () => this.tmsWaybillCategory != "DELIVERY_WAYBILL",},
            { key: 'wmsVolumn', label: 'label_tms_waybill_total_wmsVolumn', 'width': 100, 'show-overflow-tooltip': true, if: () => this.tmsWaybillCategory != "DELIVERY_WAYBILL", },
            { key: 'isBreakUp', label: 'label_tms_waybill_already_split', 'width': 100, formatter: (r, c, v) => this.$enumShow(v, 'YesOrNoEnum') },
            {
              key: 'esArrivalTime', label: 'label_expected_arrival_date', 'width': 100, 'show-overflow-tooltip': true,
              formatter: (r, c, v, i) => this.$dateFormat(v)
            },
            {
              type: 'operate', width: 210, operate: [

                {
                  type: 'more',
                  items: [
                    { label: 'label_view_detail', click: (row) => this.$router.push({ path: '/page/tms/order/waybill_detail', query: { id: row.id, code: row.waybillCode } }) },
                    { label: 'label_download_attachment', click: (row) => {this.exportAttach(row)}},
                    { label: '查看运单任务', click: (row) => this.$router.push({ path: '/page/tms/order/waybill_task_manage', query: { waybillTaskType: 'EXECUTE_TASK', waybillCode: row.waybillCode } }) },
                    {
                      label: "label_tms_waybill_volume_split",
                      disabled: (row) => !row.buttonDTOList.includes(400),
                      click: (row) => {

                        this.$refs.waybill_break_qty.show(row)
                      }
                    },
                    {
                      label: "label_tms_waybill_line_split",
                      disabled: (row) => !row.buttonDTOList.includes(450),
                      click: (row) => {
                        this.$refs.waybill_break_line.show(row)

                      }
                    },
                    {
                      label: "label_tms_waybill_cancel_split",
                      disabled: (row) => !row.buttonDTOList.includes(500),
                      click: (row) => {
                        this.clickRecallBreakUp(row)
                      }
                    },
                    {
                      label:()=>filterUploadName[this.tmsWaybillCategory] || '上传附件',
                      click: (row) => {
                        this.clickFileUpload(row)
                      }
                    },
                    {
                      label:'调度',
                      click: (row) => {
                        this.clickMergeScheduling(row)
                      }
                    },
                  ]
                }
              ]
            }
          ],

        }
        ,

      },

    };
  },
  mounted() {
  },
  methods: {
    selectable(row) {
      return row.isBreakUp != "Y" || row.parrentWaybillId != 0
    },
    cellClassName({ row, column, rowIndex, columnIndex }) {
      if (!this.selectable(row)) {
        return 'tableCell_disabled'
      }
      return 'tableCell_noHoverLight'
    },
    init_data_default(into_params) {
      if (this.tmsWaybillCategory) {
        this.$emit("tagModifyLabel", "TmsWaybillCategoryEnum__" + into_params.tmsWaybillCategory)
      }
    },
    clickTransferToPeer() {

      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      // if (this.selection.some(it => !it.buttonDTOList.includes(700))) {
      //   return this.$message.error(this.$tt('tip_part_order_status_error_no_operate'))
      // }
      this.$refs.waybill_transfer_peer.show(this.selection)
    },
    clickMergeScheduling(row) {
      let rows = row?[row] : this.selection
      if (_.isEmpty(rows)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      // if (this.selection.some(it => !it.buttonDTOList.includes(550))) {
      //   return this.$message.error(this.$tt('tip_part_order_status_error_no_operate'))
      // }
      this.$refs.waybill_merge_scheduling.show(rows,!!row)
    },
    clickBatchScheduling() {
      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      // if (this.selection.some(it => !it.buttonDTOList.includes(600))) {
      //   return this.$message.error(this.$tt('tip_part_order_status_error_no_operate'))
      // }
      this.$confirm(this.$tt('label_tms_waybill_confirm_batch_scheduling'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.submitBatchScheduling(this.selection);
      }).catch(() => {
      });
    },
    submitBatchScheduling(items) {
      this.$ajax({
        url: "/tms-api/waybill/TmsWaybillInfoOperationRpc/batchScheduling.json",
        method: 'post', type: 'json',
        loading: true,
        data: {
          waybillIdAndVersionList: items.map(it => {
            return {
              id: it.id,
              version: it.version,
            }
          }),
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.reload()
        },
      })
    },
    clickRecallScheduling() {
      if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      // if (this.selection.some(it => !it.buttonDTOList.includes(650))) {
      //   return this.$message.error(this.$tt('tip_part_order_status_error_no_operate'))
      // }
      this.$confirm(this.$tt('label_tms_waybill_confirm_cancel_scheduling'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.submitRecallScheduling(this.selection);
      }).catch(() => {
      });
    },
    submitRecallScheduling(items) {
      this.$ajax({
        url: "/tms-api/waybill/TmsWaybillInfoOperationRpc/recallScheduling.json",
        method: 'post', type: 'json',
        loading: true,
        data: {
          waybillIdAndVersionList: items.map(it => {
            return {
              id: it.id,
              version: it.version,
            }
          }),
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.reload()
        },
      })
    },

    clickRecallBreakUp(row) {


      this.$confirm(this.$tt('label_tms_waybill_confirm_cancel_split'), this.$tt('label_tip'), {
        type: 'warning'
      }).then(() => {
        this.submitRecallBreakUp(row);
      }).catch(() => {
      });
    },
    submitRecallBreakUp(row) {
      this.$ajax({
        url: "/tms-api/waybill/TmsWaybillInfoOperationRpc/recallBreakUp.json",
        method: 'post',
        loading: true,
        data: {
          id: row.id,
          version: row.version,
        },
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.reload()
        },
      })
    },
    exportExcel() {
      this.$downloadFile({
        loading: true,
        type: 'json',
        url: '/tms-api/waybill/TmsWaybillInfoQueryRpc/exportExcel.json',
        data: {
          waybillIds: this.selection.map(it => it.id),
          ...this.getSuperTablePage().getSearchFormData()
        },
      })
    },
    exportAttach(row) {
      this.$downloadFile({
        loading: true,
        type: 'json',
        method: 'post',
        data: {
          id: row.id,
        },
        url: '/tms-api/waybill/TmsWaybillInfoQueryRpc/zipDownload.json',
      })
    },
    clickFileUpload(row){
      this.$refs.waybill_file_upload.show(row)
    }
  }
};

</script>
<style></style>
